package com.okonna.selenium.tide.views;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import com.okonna.selenium.tide.Application;


public class ConfigView 
{
    private Shell shConfig;
    private Display display;
    private Configuration config;
    private Application app;

    /*
    public ConfigView(Application tmpApp)
    {
		System.out.println(this.getClass().getSimpleName() + ".ConfigView(" + tmpApp + ")");
		app = tmpApp;
		display = app.getDisplay();
		shConfig = new Shell(display);
    }
    */
    
    public ConfigView(Application tmpApp, Shell tmpParentShell)
    {
		System.out.println(this.getClass().getSimpleName() + ".ConfigView(" + tmpApp + ", " + tmpParentShell + ")");
		app = tmpApp;
		display = app.getDisplay();
		shConfig = new Shell(tmpParentShell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    }

    public Shell initView()
    {
		System.out.println(this.getClass().getSimpleName() + ".initView()");
		shConfig.setText("Selenium Tide - Options");
		shConfig.setImage(new Image(display, "selenium-rc-logo.png"));
		shConfig.setSize(320, 240);
		shConfig.setLayout(new GridLayout(2, false));		
		
    	//Server Host, Server Port, Browser Start Command, Browser URL
		final Label lblServerHost = new Label(shConfig, SWT.LEFT);
		lblServerHost.setText("Server host:");
		lblServerHost.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, true, 1, 1));
	
		final Text txtServerHost = new Text(shConfig, SWT.SINGLE);
		txtServerHost.setText("");
		txtServerHost.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, true, 1, 1));
		
		final Label lblServerPort = new Label(shConfig, SWT.LEFT);
		lblServerPort.setText("Server Port:");
		lblServerPort.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, true, 1, 1));
	
		final Text txtServerPort = new Text(shConfig, SWT.SINGLE);
		txtServerPort.setText("");
		txtServerPort.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, true, 1, 1));
		
		final Label lblBrowserStartCommand = new Label(shConfig, SWT.LEFT);
		lblBrowserStartCommand.setText("Browser Start Command:");
		lblBrowserStartCommand.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, true, 1, 1));
	
		final Text txtBrowserStartCommand = new Text(shConfig, SWT.SINGLE);
		txtBrowserStartCommand.setText("");
		txtBrowserStartCommand.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, true, 1, 1));
		
		final Label lblBrowserURL = new Label(shConfig, SWT.LEFT);
		lblBrowserURL.setText("Browser URL:");
		lblBrowserURL.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, true, 1, 1));
	
		final Text txtBrowserURL = new Text(shConfig, SWT.SINGLE);
		txtBrowserURL.setText("");
		txtBrowserURL.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, true, 1, 1));
		
		try 
		{
			config = app.getConfig();
			txtServerHost.setText(config.getString("Server.Host", "localhost"));
			txtServerPort.setText(config.getString("Server.Port", "4444"));
			txtBrowserStartCommand.setText(config.getString("Browser.StartCommand", "*firefox"));
			txtBrowserURL.setText(config.getString("Browser.URL", "http://www.example.com"));
		} 		
		catch (Exception e)
		{
		    System.out.println("\nException:\n" + e + "\n");
		}
		
		final Button btnSaveConfig = new Button(shConfig, SWT.PUSH);
		btnSaveConfig.setText("Save");
		btnSaveConfig.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, true, true, 1, 1));
		
		btnSaveConfig.addSelectionListener(new SelectionListener()
		{
		    public void widgetSelected(SelectionEvent event)
		    {
				try
				{
					config.setProperty("Server.Host", txtServerHost.getText());
					config.setProperty("Server.Port", txtServerPort.getText());
					config.setProperty("Browser.StartCommand", txtBrowserStartCommand.getText());
					config.setProperty("Browser.URL", txtBrowserURL.getText());
					((PropertiesConfiguration)config).save();
					MessageBox messageBox = new MessageBox(shConfig, SWT.OK);
					messageBox.setText("Success");
					messageBox.setMessage("Saved");
					messageBox.open();
				}
				catch (Exception e)
				{
				    System.out.println("\nException:\n" + e + "\n");
				}
		    }

		    public void widgetDefaultSelected(SelectionEvent event)
		    {
		    	System.out.println("WTF does 'widgetDefaultSelected(SelectionEvent event)' do?");
		    }
		});
		
		return shConfig;
    }
    
}
